package com.atguigu.flink0624.chapter11.function;

import com.atguigu.flink0624.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/20 10:29
 */
public class Flink04_Function_TableAgg {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        DataStreamSource<WaterSensor> stream = env.fromElements(
            new WaterSensor("sensor_1", 1000L, 10),
            new WaterSensor("sensor_1", 2000L, 20),
            new WaterSensor("sensor_1", 4000L, 40),
            new WaterSensor("sensor_1", 5000L, 50),
            new WaterSensor("sensor_2", 3000L, 30),
            new WaterSensor("sensor_2", 6000L, 60));
        Table table = tenv.fromDataStream(stream);
        tenv.createTemporaryView("t1", table);
        
        // 1. 在table api中使用
        // 1.1. 内联使用
        table
            .groupBy($("id"))
            .flatAggregate(call(Top2.class, $("vc")))
            .select($("id"), $("rank"), $("value"))
            .execute()
            .print();
        
        // 1.2. 注册后使用
        
        // 2. 在sql语句中使用
        // 目前不支持
        
    }
    
    public static class Result {
        public String rank;
        public Integer value;
        
        public Result(String rank, Integer value) {
            this.rank = rank;
            this.value = value;
        }
    }
    
    public static class FirstSecond {
        public Integer first = 0;
        public Integer second = 0;
    }
    
    public static class Top2 extends TableAggregateFunction<Result, FirstSecond> {
        
        // 初始化累加器
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 这个方法用来累加
        public void accumulate(FirstSecond acc, Integer vc) {
            if (vc > acc.first) {
                acc.second = acc.first;
                acc.first = vc;
            } else if (vc > acc.second) {
                acc.second = vc;
            }
        }
        
        // 生成表
        public void emitValue(FirstSecond acc, Collector<Result> out) {
            // out.collect 方法两次, 就表示来一条数据生产两行
            out.collect(new Result("最大", acc.first));
            if (acc.second > 0) {
                out.collect(new Result("次大", acc.second));
            }
        }
        
    }
    
}
/*
sensor_1   10

    sensor_1   最大    10
    
sensor_1   20
    sensor_1   最大    20
    sensor_1   次大    10


sensor_1   30
    sensor_1   最大    30
    sensor_1   次大    20

 */
